package com.acce.gxsc.page.supervision;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.acce.gxsc.R;
import com.acce.gxsc.common.Constants;
import com.acce.gxsc.common.DateUtils;
import com.acce.gxsc.model.bean.TypeBean;
import com.acce.gxsc.page.supervision.carInMarket.CarInMarketActivity;
import com.acce.gxsc.page.supervision.outCarDetail.OutCarDetailActivity;
import com.acce.sdk.BaseActivity;
import com.acce.sdk.base.mvp.BaseMvpFragment;
import com.acce.sdk.user.qrScan.QrCodeScanActivity;
import com.acce.sdk.utils.StringUtils;
import com.acce.sdk.utils.ToastUtils;
import com.acce.sdk.widget.adapterlv.CommonAdapter;
import com.acce.sdk.widget.adapterlv.ViewHolder;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by huxley on 2017/5/26.
 */
public class SupervisionFragment extends BaseMvpFragment<SupervisionView, SupervisionPresenter> implements SupervisionView {

    View rootView;
    @Bind(R.id.et_id)
    EditText mEtId;
    @Bind(R.id.et_car_no)
    EditText mEtCarNo;
    @Bind(R.id.et_box_no)
    EditText mEtBoxNo;
    @Bind(R.id.et_time)
    TextView mEtTime;
    @Bind(R.id.et_hang_no)
    EditText mEtHangNo;
    @Bind(R.id.et_contact_name)
    EditText mEtContactName;
    @Bind(R.id.spinner_type)
    Spinner  mSpinnerType;

    int[]            mCurrentDate;
    DatePickerDialog mDatePickerDialog;
    ArrayList<TypeBean> mTypeList;

    public static SupervisionFragment newInstance() {
        Bundle args = new Bundle();
        SupervisionFragment fragment = new SupervisionFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected SupervisionPresenter createPresenter() {
        return new SupervisionPresenter();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_supervision, container, false);
        ButterKnife.bind(this, rootView);
        init();
        setHasOptionsMenu(true);
        return rootView;
    }

    private void init() {
        mCurrentDate = DateUtils.getCurrentDates();
        mTypeList = new ArrayList<>();
        mTypeList.add(new TypeBean("100", "默认按照车牌号码查找相似车辆"));
        mTypeList.add(new TypeBean("200", "按照货柜编号查找相似未离场车辆"));
        mTypeList.add(new TypeBean("300", "按照车尾挂号查找相似未离场车辆"));
        mSpinnerType.setAdapter(new CommonAdapter<TypeBean>(getContext(), R.layout.input_spinner_item, mTypeList) {
            @Override
            protected void convert(ViewHolder viewHolder, TypeBean typeBean, int i) {
                viewHolder.setText(R.id.tv_content, typeBean.name);
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void showProgressDialog(String s, boolean b) {
        ((BaseActivity) getActivity()).showProgressDialog(s, b);
    }

    @Override
    public void hideProgressDialog() {
        ((BaseActivity) getActivity()).hideProgressDialog();
    }

    @OnClick({R.id.tv_id, R.id.et_time, R.id.iv_scanning, R.id.tv_search, R.id.tv_out_search, R.id.tv_id_1, R.id.tv_search_1})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_id: // 根据收费单查询场内车辆
                String queryId = mEtId.getText().toString();
                if (StringUtils.isEmpty(queryId)) {
                    ToastUtils.show("请输入单号");
                    return;
                }
                Intent outCarDetailIntent1 = new Intent(getActivity(), OutCarDetailActivity.class);
                outCarDetailIntent1.putExtra("queryType", mTypeList.get(mSpinnerType.getSelectedItemPosition()).no);
                outCarDetailIntent1.putExtra("queryId", queryId);
                outCarDetailIntent1.putExtra("outCarStatus", "0");
                startActivity(outCarDetailIntent1);
                break;
            case R.id.tv_id_1: // 车辆历史记录
                String queryId1 = mEtId.getText().toString();
                if (StringUtils.isEmpty(queryId1)) {
                    ToastUtils.show("请输入单号");
                    return;
                }
                Intent outCarDetailIntent3 = new Intent(getActivity(), OutCarDetailActivity.class);
                outCarDetailIntent3.putExtra("queryType", mTypeList.get(mSpinnerType.getSelectedItemPosition()).no);
                outCarDetailIntent3.putExtra("queryId", queryId1);
                outCarDetailIntent3.putExtra("outCarStatus", "1");
                startActivity(outCarDetailIntent3);
                break;
            case R.id.et_time:
                if (mDatePickerDialog == null) {
                    mDatePickerDialog = new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {
                        @Override
                        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                            mCurrentDate[0] = year;
                            mCurrentDate[1] = monthOfYear + 1;
                            mCurrentDate[2] = dayOfMonth;
                            mEtTime.setText(String.format("%d-%02d-%02d", mCurrentDate[0], mCurrentDate[1], mCurrentDate[2]));
                        }
                    }, mCurrentDate[0], mCurrentDate[1], mCurrentDate[2]);
                    mDatePickerDialog.getDatePicker().setMaxDate(DateUtils.getCurrentTimeMillis1());
                    mDatePickerDialog.setTitle("请选择日期");
                }
                mDatePickerDialog.show();
                break;
            case R.id.iv_scanning:
                getActivity().startActivityForResult(
                        (new Intent(getActivity(), QrCodeScanActivity.class)).putExtra(Constants.BaseKey.IS_BARCODE, true),
                        1001);
                break;
            case R.id.tv_search: //根据其他条件查询场内车辆
                Intent outCarDetailIntent = new Intent(getActivity(), OutCarDetailActivity.class);
                outCarDetailIntent.putExtra("plateNum", mEtCarNo.getText().toString());
                outCarDetailIntent.putExtra("containNo", mEtBoxNo.getText().toString());
                outCarDetailIntent.putExtra("hangNum", mEtHangNo.getText().toString());
                outCarDetailIntent.putExtra("contactName", mEtContactName.getText().toString());
                outCarDetailIntent.putExtra("outCarStatus", "0");
                startActivity(outCarDetailIntent);
                break;
            case R.id.tv_search_1: //车辆历史记录
                String plateNum1 = mEtCarNo.getText().toString();
                String containNo1 = mEtBoxNo.getText().toString();
                String hangNum1 = mEtHangNo.getText().toString();
                String contactName1 = mEtContactName.getText().toString();
                if (StringUtils.isEmpty(plateNum1) && StringUtils.isEmpty(containNo1) && StringUtils.isEmpty(hangNum1) && StringUtils.isEmpty(contactName1)) {
                    ToastUtils.show("至少填写一项信息");
                    return;
                }
                Intent outCarDetailIntent2 = new Intent(getActivity(), OutCarDetailActivity.class);
                outCarDetailIntent2.putExtra("plateNum", plateNum1);
                outCarDetailIntent2.putExtra("containNo", containNo1);
                outCarDetailIntent2.putExtra("hangNum", hangNum1);
                outCarDetailIntent2.putExtra("contactName", contactName1);
                outCarDetailIntent2.putExtra("outCarStatus", "1");
                startActivity(outCarDetailIntent2);
                break;
            case R.id.tv_out_search: // 场内全部车辆清单
                loadCarInMarket();
//                Intent intent1 = new Intent(getActivity(), ToOutCarListActivity.class);
//                intent1.putExtra("carNo", "");
//                intent1.putExtra("boxNo", "");
//                intent1.putExtra("time", "");
//                intent1.putExtra("carHangNo", "");
//                intent1.putExtra("contactName", "");
//                intent1.putExtra("outCarStatus", "0");
//                startActivity(intent1);
                break;
        }
    }

    /**
     * 场内全部车辆清单
     */
    private void loadCarInMarket() {
        startActivity(new Intent(getContext(), CarInMarketActivity.class));
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data == null) {
            return;
        }
        if (resultCode != Activity.RESULT_OK) {
            return;
        }
        switch (requestCode) {
            case 1001:
                String qrScanResult = data.getStringExtra("qrScanResult");
                mEtId.setText(qrScanResult);
                Intent outCarDetailIntent = new Intent(getActivity(), OutCarDetailActivity.class);
                outCarDetailIntent.putExtra("queryType", mTypeList.get(mSpinnerType.getSelectedItemPosition()).no);
                outCarDetailIntent.putExtra("queryId", mEtId.getText().toString());
                outCarDetailIntent.putExtra("outCarStatus", "0");
                startActivity(outCarDetailIntent);
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
